<?php
namespace app\api\model;

class IndexModel extends BaseModel
{
    public function map($longitude,$latitude,$distance){
        $degree = (24901*1609)/360.0;
        $dpmLat = 1/$degree;
        $radius= (int)$distance;
        $radiusLat = $dpmLat*$radius;
        $minLat = $latitude - $radiusLat;
        // 最小纬度
        $maxLat = $latitude + $radiusLat;
        // 最大纬度
        $mpdLng = $degree*cos($latitude * (pi()/180));
        $dpmLng = 1 / $mpdLng;
        $radiusLng = $dpmLng*$radius;
        $minLng = $longitude - $radiusLng;
        // 最小经度
        $maxLng = $longitude + $radiusLng;
        // 最大经度
        /** 返回范围数组 */
        $scope = array('minLat'=>$minLat,'maxLat'=>$maxLat,'minLng'=>$minLng,'maxLng'=>$maxLng);
        return $scope;
    }
    //计算距离
    public function GetDistance($lat1, $lng1, $lat2, $lng2, $len_type = 1, $decimal = 2)
    {

        $earth = 6378.137;
//        $pi = 3.1415926;
        $radLat1 = $lat1 * PI ()/ 180.0;   //PI()圆周率
        $radLat2 = $lat2 * PI() / 180.0;
        $a = $radLat1 - $radLat2;
        $b = ($lng1 * PI() / 180.0) - ($lng2 * PI() / 180.0);
        $s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));
        $s = $s * $earth;
        $s = round($s * 1000);
        if ($len_type > 1)
        {
            $s /= 1000;
        }

        return round($s, $decimal);
    }
}